package system

import (
	"github.com/gookit/color"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
	"server/global"
	sysModel "server/model/system"
	"time"
)

func InitDatabase() {
	global.GvaDb.AutoMigrate(
		sysModel.SysUser{},
		sysModel.SysRole{},
		sysModel.SysPermission{},
		sysModel.SysOperationLog{},
		sysModel.SysFile{},
		sysModel.SysMenuGroup{},
		sysModel.SysMenu{},
		sysModel.SysConfig{},
		sysModel.SysJwtBlacklist{},
		sysModel.SysCasbinRule{},
	)
	//初始化角色数据
	InitRoleList()
	//初始化系统用户信息
	InitUserList()
	//初始化黑名单信息
	InitJwtBlackList()
	//初始化权限
	InitPermList()
	//初始化菜单数据
	InitMenuGroupList()
	InitMenuList()
}

func InitRoleList() error {
	roles := []sysModel.SysRole{
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1, Name: "super", DisplayName: "超管"},
	}
	return global.GvaDb.Transaction(func(tx *gorm.DB) error {
		for _, role := range roles {
			if role.ID > 0 && tx.Where(&sysModel.SysRole{ID: role.ID}).First(&sysModel.SysRole{}).RowsAffected > 0 {
				color.Danger.Println("\n[Mysql] --> sys_role 表的初始数据已存在!")
			} else {
				if err := tx.Create(&role).Error; err != nil { // 遇到错误时回滚事务
					return err
				}
				color.Info.Println("\n[Mysql] --> sys_role 表初始数据成功!")
			}
		}
		return nil
	})
}

//初始化用户数据
func InitUserList() error {
	var users = []sysModel.SysUser{
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1, UUID: uuid.NewV4(), Email: "admin@163.com", Password: UserServiceApp.Password("123456"), Name: "超级管理员", Avatar: "https://file.suofeiya.com.cn/v/9ed2bec6-9416-498b-8ce9-ece57bae6230"},
	}
	return global.GvaDb.Transaction(func(tx *gorm.DB) error {
		for _, user := range users {
			if user.ID > 0 && tx.Where(&sysModel.SysUser{ID: user.ID}).First(&sysModel.SysUser{}).RowsAffected > 0 {
				color.Danger.Println("\n[Mysql] --> sys_user 表的初始数据已存在!")
			} else {
				if err := tx.Create(&user).Error; err != nil { // 遇到错误时回滚事务
					return err
				}
				color.Info.Println("\n[Mysql] --> sys_user 表初始数据成功!")
			}
		}
		//用户初始化超管角色
		if _, err := CasbinServiceApp.Casbin().AddRoleForUser("1", "super"); err != nil {
			return err
		}
		return nil
	})
}

//初始化菜单分组
func InitMenuGroupList() error {
	groups := []sysModel.SysMenuGroup{
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1, GroupKey: MENU_GROUP_KEY_ADMIN, GroupName: "后台菜单"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 2, GroupKey: MENU_GROUP_KEY_FRONT, GroupName: "前台菜单"},
	}
	return global.GvaDb.Transaction(func(tx *gorm.DB) error {
		for _, group := range groups {
			if group.ID > 0 && tx.Where(&sysModel.SysMenuGroup{ID: group.ID}).First(&sysModel.SysMenuGroup{}).RowsAffected > 0 {
				color.Danger.Println("\n[Mysql] --> sys_menu_group 表的初始数据已存在!")
			} else {
				if err := tx.Create(&group).Error; err != nil { // 遇到错误时回滚事务
					return err
				}
				color.Info.Println("\n[Mysql] --> sys_menu_group 表初始数据成功!")
			}
		}
		return nil
	})
}

//初始化菜单列表
func InitMenuList() error {
	menus := []sysModel.SysMenu{
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1, ParentId: 0, GroupId: 1, MenuKey: "dashboard", MenuName: "控制面板", PermId: 0, Component: "RouteView", Icon: "desktop"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 2, ParentId: 0, GroupId: 1, MenuKey: "system", MenuName: "系统管理", PermId: 0, Component: "PageView", Icon: "setting"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 3, ParentId: 0, GroupId: 1, MenuKey: "account", MenuName: "用户", PermId: 0, Component: "PageView", Icon: "user", Hidden: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 4, ParentId: 3, GroupId: 1, MenuKey: "settings", MenuName: "个人设置", PermId: 0, Component: "AccountSetting", Icon: "setting", Redirect: "/account/settings/basic", Hidden: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 101, ParentId: 1, GroupId: 1, MenuKey: "workplace", MenuName: "仪表盘", PermId: 111, Component: "Workplace", Icon: "dashboard"},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 102, ParentId: 2, GroupId: 1, MenuKey: "menugroup", MenuName: "菜单分组管理", PermId: 4, Component: "SysMenuGroup", Icon: "appstore"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 103, ParentId: 2, GroupId: 1, MenuKey: "menu", MenuName: "菜单管理", PermId: 5, Component: "SysMenu", Icon: "cluster"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 104, ParentId: 2, GroupId: 1, MenuKey: "permission", MenuName: "权限管理", PermId: 3, Component: "SysPermission", Icon: "branches"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 105, ParentId: 2, GroupId: 1, MenuKey: "role", MenuName: "角色管理", PermId: 6, Component: "SysRole", Icon: "team"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 106, ParentId: 2, GroupId: 1, MenuKey: "user", MenuName: "用户管理", PermId: 7, Component: "SysUser", Icon: "user"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 107, ParentId: 2, GroupId: 1, MenuKey: "operationlog", MenuName: "日志管理", PermId: 9, Component: "SysOperationLog", Icon: "project"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 108, ParentId: 2, GroupId: 1, MenuKey: "file", MenuName: "文件管理", PermId: 10, Component: "SysFile", Icon: "file-search"},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 109, ParentId: 3, GroupId: 1, MenuKey: "center", MenuName: "个人中心", PermId: 11, Component: "AccountCenter", Icon: "user", Hidden: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 110, ParentId: 4, GroupId: 1, MenuKey: "basic", MenuName: "基本设置", PermId: 1021, Component: "BasicSettings", Icon: "tool"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 111, ParentId: 4, GroupId: 1, MenuKey: "security", MenuName: "安全设置", PermId: 1022, Component: "SecuritySettings", Icon: "insurance"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 112, ParentId: 4, GroupId: 1, MenuKey: "custom", MenuName: "个性化", PermId: 1023, Component: "CustomSettings", Icon: "heart"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 113, ParentId: 4, GroupId: 1, MenuKey: "binding", MenuName: "账户绑定", PermId: 1024, Component: "BindingSettings", Icon: "paper-clip"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 114, ParentId: 4, GroupId: 1, MenuKey: "notification", MenuName: "新消息通知", PermId: 1025, Component: "NotificationSettings", Icon: "sound"},
	}
	return global.GvaDb.Transaction(func(tx *gorm.DB) error {
		for _, menu := range menus {
			if menu.ID > 0 && tx.Where(&sysModel.SysMenu{ID: menu.ID}).First(&sysModel.SysMenu{}).RowsAffected > 0 {
				color.Danger.Println("\n[Mysql] --> sys_menu 表的初始数据已存在!")
			} else {
				if err := tx.Create(&menu).Error; err != nil { // 遇到错误时回滚事务
					return err
				}
				color.Info.Println("\n[Mysql] --> sys_menu 表初始数据成功!")
			}
		}
		return nil
	})
}

//初始化权限列表
func InitPermList() error {
	perms := []sysModel.SysPermission{
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1, ParentId: 0, Name: "admin", DisplayName: "后台权限", OrderNum: 1, IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 2, ParentId: 1, Name: "admin.base", DisplayName: "【系统】基础部分"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 3, ParentId: 1, Name: "admin.sys-permission", DisplayName: "【系统】权限管理", OrderNum: 5},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 4, ParentId: 1, Name: "admin.sys-menu-group", DisplayName: "【系统】菜单分组管理", OrderNum: 3},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 5, ParentId: 1, Name: "admin.sys-menu", DisplayName: "【系统】菜单管理", OrderNum: 4},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 6, ParentId: 1, Name: "admin.sys-role", DisplayName: "【系统】角色管理", OrderNum: 6},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 7, ParentId: 1, Name: "admin.sys-user", DisplayName: "【系统】用户管理", OrderNum: 7},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 8, ParentId: 1, Name: "admin.dashboard", DisplayName: "【系统】控制面板", OrderNum: 2, IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 9, ParentId: 1, Name: "admin.sys-opt-log", DisplayName: "【系统】日志管理", OrderNum: 8},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 10, ParentId: 1, Name: "admin.sys-file", DisplayName: "【系统】文件管理", OrderNum: 9},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 11, ParentId: 1, Name: "admin.user", DisplayName: "【系统】用户", OrderNum: 1},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 12, ParentId: 11, Name: "admin.user.update", DisplayName: "更新用户信息", IsPublic: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 111, ParentId: 2, Name: "admin.dashboard.index", DisplayName: "主面板"},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 121, ParentId: 2, Name: "admin.base.login", DisplayName: "登录", IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 122, ParentId: 2, Name: "admin.base.captcha", DisplayName: "验证码获取", IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 123, ParentId: 2, Name: "admin.base.logout", DisplayName: "退出登录", IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 124, ParentId: 2, Name: "admin.base.user-info", DisplayName: "获取用户信息", OrderNum: 1, IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 125, ParentId: 2, Name: "admin.base.get-menu", DisplayName: "获取菜单列表", OrderNum: 2, IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 126, ParentId: 2, Name: "admin.base.user-update", DisplayName: "用户资料更新", OrderNum: 2, IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 127, ParentId: 2, Name: "admin.base.user-reset-pwd", DisplayName: "重置密码", IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 128, ParentId: 2, Name: "admin.base.geographic", DisplayName: "省市区数据", IsPublic: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 131, ParentId: 3, Name: "admin.sys-permission.index", DisplayName: "列表"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 132, ParentId: 3, Name: "admin.sys-permission.save", DisplayName: "新增/编辑", OrderNum: 1},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 133, ParentId: 3, Name: "admin.sys-permission.destroy", DisplayName: "删除", OrderNum: 3},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 134, ParentId: 3, Name: "admin.sys-permission.set-order", DisplayName: "排序", OrderNum: 4},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 141, ParentId: 4, Name: "admin.sys-menu-group.index", DisplayName: "列表", OrderNum: 2},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 142, ParentId: 4, Name: "admin.sys-menu-group.save", DisplayName: "新增/编辑", OrderNum: 3},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 143, ParentId: 4, Name: "admin.sys-menu-group.destroy", DisplayName: "删除", OrderNum: 4},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 151, ParentId: 5, Name: "admin.sys-menu.index", DisplayName: "列表", OrderNum: 7},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 152, ParentId: 5, Name: "admin.sys-menu.save", DisplayName: "新增/编辑", OrderNum: 8},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 153, ParentId: 5, Name: "admin.sys-menu.destroy", DisplayName: "删除", OrderNum: 10},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 161, ParentId: 6, Name: "admin.sys-role.index", DisplayName: "列表", OrderNum: 2},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 162, ParentId: 6, Name: "admin.sys-role.save", DisplayName: "新增/编辑", OrderNum: 4},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 163, ParentId: 6, Name: "admin.sys-role.destroy", DisplayName: "删除", OrderNum: 5},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 164, ParentId: 6, Name: "admin.sys-menu.set-order", DisplayName: "排序", OrderNum: 11},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 171, ParentId: 7, Name: "admin.sys-user.index", DisplayName: "列表", OrderNum: 25},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 172, ParentId: 7, Name: "admin.sys-user.save", DisplayName: "添加/编辑", OrderNum: 26},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 173, ParentId: 7, Name: "admin.sys-user.disable", DisplayName: "禁用用户", OrderNum: 27},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 174, ParentId: 7, Name: "admin.sys-user.enable", DisplayName: "激活用户", OrderNum: 28},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 181, ParentId: 9, Name: "admin.sys-opt-log.index", DisplayName: "列表", OrderNum: 9},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 182, ParentId: 9, Name: "admin.sys-opt-log.check-params", DisplayName: "查看请求参数", OrderNum: 10},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 191, ParentId: 10, Name: "admin.sys-file.index", DisplayName: "文件列表"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 192, ParentId: 10, Name: "admin.sys-file.webupload", DisplayName: "文件上传", IsPublic: true},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 193, ParentId: 10, Name: "admin.sys-file.ueditorupload", DisplayName: "百度富文本文件上传", IsPublic: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1011, ParentId: 11, Name: "admin.user.art-list", DisplayName: "文章列表", IsPublic: true},

		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1021, ParentId: 12, Name: "admin.account.base", DisplayName: "基本设置"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1022, ParentId: 12, Name: "admin.account.security", DisplayName: "安全设置"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1023, ParentId: 12, Name: "admin.account.custom", DisplayName: "个性化"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1024, ParentId: 12, Name: "admin.account.binding", DisplayName: "账户绑定"},
		{GvaModel: global.GvaModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ID: 1025, ParentId: 12, Name: "admin.account.notification", DisplayName: "新消息通知"},
	}
	return global.GvaDb.Transaction(func(tx *gorm.DB) error {
		for _, perm := range perms {
			if perm.ID > 0 && tx.Where(&sysModel.SysPermission{ID: perm.ID}).First(&sysModel.SysPermission{}).RowsAffected > 0 {
				color.Danger.Println("\n[Mysql] --> sys_permission 表的初始数据已存在!")
			} else {
				if err := tx.Create(&perm).Error; err != nil { // 遇到错误时回滚事务
					return err
				}
				color.Info.Println("\n[Mysql] --> sys_permission 表初始数据成功!")
			}
			//超管权限初始化
			if _, err := CasbinServiceApp.Casbin().AddPolicy("super", "admin", perm.Name, "allow"); err != nil {
				return err
			}
		}
		return nil
	})
}

//初始化用户权限
func InitCasbin() {
	//CasbinServiceApp.Casbin().AddGroupingPolicy()
}
